// 学校
import axios from '../../resource'
import { format, empty } from '../../utils/index'
import { Notification } from 'element-ui'

// 参数处理
const paramsHandle = (params) => {
  return {
    "cname": params.cname,
    "period":`[${params.period}]`,
    "address": params.address,
    "badge": params.badge,
    "province": params.area[0],
    "district": params.area[2],
    "city": params.area[1],
    "motto": params.motto,
    "openSchool": {
      "course": JSON.stringify(params.course),
      "starttime": params.date[0],
      "endtime": params.date[1],
      "number": params.number,
      "times": params.times,
      "domain": params.domain,
      "opentime": params.date[0]
    }
  }
}

const state = {
  stageList: [], // 学段
  subjectsList: [], // 学科
  areaList: [], // 地区
  schoolList: [], // 学校列表
  provinceList: [], // 省份列表
  search: {
    "cname": "",
    "province": "",
    "pageSize":10,
    "pageNum":1
  },
  nameStatus: false,
  total: 0 // 条数
}


const mutations = {
  setState (state, opts) {
    Object.assign(state, opts)
  },

  setSearch (state, opts) {
    Object.assign(state.search, opts)
  },

  // 修改同步状态
  updateList (state, {index, status}) {
    state.schoolList[index]['status'] = status;
  }
}


const actions = {

  // 获取全量地区
  getAreaAll ({state, commit, dispatch}) {
    axios({
      url: `/api/commons/action?action=getAreaAll`
    }).then(response => {
      commit('setState', {areaList: response.data})
    })
  },

  // 获取省份列表
  getProvince ({state, commit, dispatch}) {
    axios({
      url: `/api/commons/action?action=getArea`
    }).then(response => {
      commit('setState', {provinceList: response.data})
    })
  },

  // 获取学科
  getSubject ({state, commit, dispatch}, id) {
    axios({
      url: `/api/commons/action?action=getDictionary&subjects=${id}`
    }).then(response => {
      commit('setState', {subjectsList: response.data})
    })
  },

  // 获取学段
  getStage ({state, commit, dispatch}) {
    axios({
      url: `/api/commons/action?action=getDictionary`
    }).then(response => {
      commit('setState', {stageList: response.data})
    })
  },

  // 创建学校
  create ({state, commit, dispatch}, {params, cb}) {
    commit('setLoading', true, { root: true });
    let body = paramsHandle(params)
    axios({
      url: `/api/schools`,
      method: "POST",
      data: body
    }).then(response => {
      commit('setLoading', false, { root: true });
      cb && cb()
    })
  },

  // 学校信息更新
  update ({state, commit, dispatch}, {params, cb}) {
    let schoolId = params.id;
    commit('setLoading', true, { root: true });
    let body = paramsHandle(params);
    axios({
      url: `/api/schools/${schoolId}`,
      method: "POST",
      data: body
    }).then(response => {
      commit('setLoading', false, { root: true });
      cb && cb()
    })
  },

  // 开通学校信息详情
  show ({state, commit, dispatch}, {id, cb}) {
    commit('setLoading', true, { root: true });
    axios({
      url: `/api/schools/${id}`
    }).then(response => {
      let params = response.data;
      let period = JSON.parse(params.period)[0];
      cb({
        "cname": params.cname,
        "period": JSON.stringify(period),
        "address": params.address,
        "badge": params.badge,
        "area": [params.province, params.city, params.district],
        "motto": params.motto,
        "course": JSON.parse(params.openSchool.course),
        "date": [params.openSchool.starttime, params.openSchool.endtime],
        "number": params.openSchool.number,
        "times": params.openSchool.times,
        "domain": params.openSchool.domain
      })
      commit('setLoading', false, { root: true });
      dispatch('getSubject', period.id)
    })
  },

  // 学校列表
  index ({state, commit, dispatch}) {
    commit('setLoading', true, { root: true });
    axios({
      url: '/api/schools',
      data: empty(state.search)
    }).then(response => {
      let data = response.data;
      commit('setLoading', false, { root: true });
      let schoolList = data.list.map(item => {
        let province, city, period;
        try {
          province = JSON.parse(item.province).name
          city = JSON.parse(item.city).name
          period = JSON.parse(item.period)[0].itemname
        } catch (e) {
          province = item.province
          city = item.city
          period = item.period
        }
        return {
          id: item.id,
          cname: item.cname,
          province: province,
          city: city,
          period: period,
          number: item.openSchool.number,
          lasttimes: item.openSchool.lasttimes,
          starttime: item.openSchool.starttime,
          endtime: item.openSchool.endtime,
          opentime: item.openSchool.opentime,
          status: item.openSchool.status
        }
      })
      commit('setState', {schoolList: schoolList, total: data.total})
    })
  },

  // 学校名称验证
  isExist ({state, commit}, {cname, cb}) {
    axios({
      url: '/api/schools/action',
      data: {
        action: 'isExist',
        cname: cname
      }
    }).then(response => {
      commit('setState', {nameStatus: response.data})
    })
  },

  // 禁用、启用
  edit ({state, commit}, params) {
    let status = params.status ? 0 : 1;
    axios({
      url: `/api/schools/${params.id}/edit`,
      data: {
        status: status
      }
    }).then(response => {
      Notification({
        message: '操作成功',
        type: "success"
      })
      commit('updateList', {index: params.index, status: status})
    })
  }
}


export default {
  namespaced: true,
  state,
  actions,
  mutations
}
